/*
 * @FilePath: /tads/server/initialize/db.go
 * @Author: wnina0x0
 * @Date: 2025-04-03 14:59:55
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
package initialize

import (
	"log"
	"tads/server/app/business/models"
	"tads/server/global"
	"time"

	"gorm.io/gorm"
)

func Gorm() *gorm.DB {
	switch *global.TADS_DBTYPE {
	case "postgres":
		global.TADS_ACTIVE_DBNAME = &global.TADS_CONFIG.Postgres.DbName
		return GormPostgres()
	default:
		return nil
	}
}

func RegisterTable() {
	db := global.TADS_DB
	if db == nil {
		log.Printf("数据库连接为nil，请先初始化数据库连接\n")
		return
	}
	//里面放数据库结构体
	err := db.AutoMigrate(
		&models.TacticsType{},
		&models.Tactics{},
		&models.BusType{},
		&models.TacticsTopo{},
		&models.Icon{},
	)
	sqlDB, err := db.DB()
	if err != nil {
		log.Printf("数据库DB错误\n")
	}
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetConnMaxLifetime(time.Hour)

	if err != nil {
		log.Printf("初始化失败\n")
	}
}
